package com.tiantian.quanminxiu.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.presenter.LoadingPagePresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.view.LoadingPageView;

import java.util.ArrayList;
import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by admin on 2016/9/18.
 */
public class Activity_LoadingPage extends ActivityBase implements LoadingPageView{

    private LoadingPagePresent present;
    private ViewPager vpWelcome;
    private ViewpagerAdaper viewpagerAdaper;
    private List<View> list;
    private int[] datas = new int[]{R.drawable.bg_welcome1,R.drawable.bg_welcome2,R.drawable.bg_welcome3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loadingpage);

        Init();

        InitView();

        present = new LoadingPagePresent();
        present.attachView(this);
    }

    @Override
    public void InitView() {
    }

    @Override
    public void initViewPager(){
        LayoutInflater inflater=LayoutInflater.from(this);
        list=new ArrayList<View>();
        for (int i=0;i<datas.length;i++) {
            View view = inflater.inflate(R.layout.activity_login_welcome_item,null);
            ImageView imageView = (ImageView) view.findViewById(R.id.mImageView);
            imageView.setImageResource(datas[i]);
            list.add(view);
        }
        viewpagerAdaper=new ViewpagerAdaper(list,this);
        vpWelcome= (ViewPager) findViewById(R.id.vpWelcome);
        vpWelcome.setAdapter(viewpagerAdaper);
        vpWelcome.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//监听ViewPager
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (list!=null&&position==list.size()-1) {
                    View view = list.get(position);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            APPUtils.setFirstStart(thisContext,false);
                            JumpLogin();
                        }
                    });
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void PageInit() {
        present.initLoadingPage();
    }

    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    public void JumpLogin() {
        APPUtils.LoginSelect_To(thisActivity);
        finish();
    }

    @Override
    public void JumpMain() {
        APPUtils.Main_To(thisActivity);
        finish();
    }

    @Override
    public void ShowUpDateDialog(int User_ID,String url) {
        SweetDialog_Init(SweetAlertDialog.WARNING_TYPE)
                .setTitleText("提示？")
                .setContentText("新版上线,是否立即更新?")
                .setConfirmText("更新")
                .setCancelText("取消")
                .setConfirmClickListener(sDialog -> {
                    if (!TextUtils.isEmpty(url)) {
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(url);
                        intent.setData(content_url);
                        thisContext.startActivity(intent);
                        finish();
                    } else {
                        Loading_Close();
                        if (User_ID != -1) {
                            JumpMain();
                        } else {
                            JumpLogin();
                        }
                    }
                }).setCancelClickListener(sDialog -> {
                    Loading_Close();
                    if (User_ID != -1){
                      JumpMain();
                    }else {
                        JumpLogin();
                    }
                }).setCancelable(false);
        SweetDialog_Show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        present.detachView();
    }

    //定义一个内部类用作adaper
    public class ViewpagerAdaper extends PagerAdapter{
        private List<View> viewList;
        private Context context;

        public ViewpagerAdaper(List<View> viewList, Context context) {
            this.viewList= viewList;
            this.context= context;
        }

        @Override
        //销毁一个View
        public void destroyItem(ViewGroup Container, int position, Object object) {
            Container.removeView(viewList.get(position));
        }

        @Override
        //加载View
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        //返回当前View的数量
        public int getCount() {
            return viewList.size();
        }

        @Override
        //判断当前的View是不是我们需要的一个对象
        public boolean isViewFromObject(View view, Object object) {
            return (view==object);
        }
    }
}
